# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@io_bazel_rules_docker//container:container.bzl", "container_bundle")
load("@io_bazel_rules_docker//contrib:push-all.bzl", "container_push")
load("//bazel:images.bzl", "generate_cloud_yamls", "image_map_with_bundle_version")

package(default_visibility = ["//visibility:public"])

CLOUD_IMAGE_MAP = {
    "gcr.io/pixie-oss/pixie-dev/cloud/api_server_image": "//src/cloud/api:api_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/artifact_tracker_server_image": "//src/cloud/artifact_tracker:artifact_tracker_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/auth_server_image": "//src/cloud/auth:auth_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/config_manager_server_image": "//src/cloud/config_manager:config_manager_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/dnsmgr_server_image": "//src/cloud/dnsmgr:dnsmgr_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/indexer_server_image": "//src/cloud/indexer:indexer_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/job/create_admin_job_image": "//src/cloud/jobs/create_admin_user:create_admin_job_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/metrics_server_image": "//src/cloud/metrics:metrics_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/profile_server_image": "//src/cloud/profile:profile_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/project_manager_server_image": "//src/cloud/project_manager:project_manager_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/proxy_server_image": "//src/cloud/proxy:proxy_prod_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/scriptmgr_server_image": "//src/cloud/scriptmgr:scriptmgr_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/vzconn_server_image": "//src/cloud/vzconn:vzconn_server_image",
    "gcr.io/pixie-oss/pixie-dev/cloud/vzmgr_server_image": "//src/cloud/vzmgr:vzmgr_server_image",
}

public_image_replacement = {"gcr.io/pixie-oss/pixie-dev": "gcr.io/pixie-oss/pixie-prod"}

proprietary_image_replacement = {"gcr.io/pixie-oss/pixie-dev": "gcr.io/pl-dev-infra"}

generate_cloud_yamls(
    name = "staging_cloud_yamls",
    srcs = glob(["**/*.yaml"]),
    out = "pixie_staging_cloud.yaml",
    image_map = CLOUD_IMAGE_MAP,
    replace = proprietary_image_replacement,
    yaml_dir = "staging",
)

generate_cloud_yamls(
    name = "prod_cloud_yamls",
    srcs = glob(["**/*.yaml"]),
    out = "pixie_prod_cloud.yaml",
    image_map = CLOUD_IMAGE_MAP,
    replace = proprietary_image_replacement,
    yaml_dir = "prod",
)

container_bundle(
    name = "staging_cloud_images_bundle",
    images = image_map_with_bundle_version(
        image_map = CLOUD_IMAGE_MAP,
        replace = proprietary_image_replacement,
        tag_latest = False,
    ),
)

container_bundle(
    name = "prod_cloud_images_bundle",
    images = image_map_with_bundle_version(
        image_map = CLOUD_IMAGE_MAP,
        replace = proprietary_image_replacement,
        tag_latest = False,
    ),
)

container_bundle(
    name = "public_cloud_images_bundle",
    images = image_map_with_bundle_version(
        image_map = CLOUD_IMAGE_MAP,
        replace = public_image_replacement,
        tag_latest = True,
    ),
)

container_push(
    name = "cloud_images_push",
    bundle = select({
        ":prod": ":prod_cloud_images_bundle",
        ":public": ":public_cloud_images_bundle",
        "//conditions:default": ":staging_cloud_images_bundle",
    }),
    format = "Docker",
)

config_setting(
    name = "public",
    values = {
        "define": "public=True",
    },
)

config_setting(
    name = "prod",
    values = {
        "define": "prod=True",
    },
)
